package com.sqf.soft.service.impl;

import com.sqf.soft.dao.IClassifyDao;
import com.sqf.soft.dao.IStatementDao;
import com.sqf.soft.dao.IUserDao;
import com.sqf.soft.entity.Classify;
import com.sqf.soft.entity.Statement;
import com.sqf.soft.entity.User;
import com.sqf.soft.service.IStatementService;
import com.sqf.soft.utils.CommonUtils;
import com.sqf.soft.utils.PageBean;
import com.sqf.soft.utils.ResultMap;
import com.sqf.soft.utils.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StatementService implements IStatementService {

    private final IStatementDao statementDao;
    private final IClassifyDao classifyDao;
    private final IUserDao userDao;

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    public StatementService(IStatementDao statementDao, IClassifyDao classifyDao, IUserDao userDao) {
        this.statementDao = statementDao;
        this.classifyDao = classifyDao;
        this.userDao = userDao;
    }

    @Override
    public Map<String, Object> findStatementByPage(PageBean pageBean) {
        try {
            List<Statement> statementList = statementDao.findStatementByPage(pageBean);
            int amount = statementDao.findAmountByPage(pageBean);
            return ResultMap.getPage(amount, statementList);
        } catch (Exception e) {
            log.error("StatementService/findStatementByPage", e);
            return ResultMap.getPage(SystemConstant.SELECT_EXCEPTION);
        }
    }

    @Override
    public Map<String, Object> createStatement(Statement statement) {
        if (CommonUtils.isBlank(statement.getClassifyId())) {
            return ResultMap.operate(SystemConstant.STATEMENT_CLASSIFY_IS_NULL);
        }
        if (CommonUtils.isBlank(statement.getMoney() + "")) {
            return ResultMap.operate(SystemConstant.STATEMENT_MONEY_IS_NULL);
        }
        if (CommonUtils.isBlank(statement.getUserId())) {
            return ResultMap.operate(SystemConstant.STATEMENT_USER_IS_NULL);
        }
        try {
            Classify classify = classifyDao.findClassifyById(statement.getClassifyId());
            if (classify == null) {
                return ResultMap.operate(SystemConstant.STATEMENT_CLASSIFY_IS_NULL);
            }
            User user = userDao.findUserById(statement.getUserId());
            if (user == null) {
                return ResultMap.operate(SystemConstant.STATEMENT_USER_IS_NULL);
            }
            statement.setId(CommonUtils.createKeyId());
            int code = statementDao.createStatement(statement);
            if (code == SystemConstant.NO) {
                return ResultMap.operate(SystemConstant.INSERT_EXCEPTION);
            }
            return ResultMap.operate(SystemConstant.SUCCESS);
        } catch (Exception e) {
            log.error("StatementService/createStatement", e);
            return ResultMap.operate(SystemConstant.INSERT_EXCEPTION);
        }
    }

    @Override
    public Map<String, Object> updateStatement(Statement statement) {
        if (CommonUtils.isBlank(statement.getClassifyId())) {
            return ResultMap.operate(SystemConstant.STATEMENT_CLASSIFY_IS_NULL);
        }
        if (CommonUtils.isBlank(statement.getMoney() + "")) {
            return ResultMap.operate(SystemConstant.STATEMENT_MONEY_IS_NULL);
        }
        try {
            Classify classify = classifyDao.findClassifyById(statement.getClassifyId());
            if (classify == null) {
                return ResultMap.operate(SystemConstant.STATEMENT_CLASSIFY_IS_NULL);
            }
            int code = statementDao.updateStatement(statement);
            if (code == SystemConstant.NO) {
                return ResultMap.operate(SystemConstant.UPDATE_EXCEPTION);
            }
            return ResultMap.operate(SystemConstant.SUCCESS);
        } catch (Exception e) {
            log.error("StatementService/updateStatement", e);
            return ResultMap.operate(SystemConstant.UPDATE_EXCEPTION);
        }
    }

    @Override
    public Map<String, Object> deleteStatement(Statement statement) {
        try {
            int code = statementDao.deleteStatement(statement);
            if (code == SystemConstant.NO) {
                return ResultMap.operate(SystemConstant.DELETE_EXCEPTION);
            }
            return ResultMap.operate(SystemConstant.SUCCESS);
        } catch (Exception e) {
            log.error("StatementService/deleteStatement", e);
            return ResultMap.operate(SystemConstant.DELETE_EXCEPTION);
        }
    }
}
